package com.gxa.admin.pojo;


import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import javax.persistence.*;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author IKE <hp1041735092@outlook.jp>
 * @date 21/3/2024
 */

@Slf4j
@EqualsAndHashCode(callSuper = false)
@Data
@Entity
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "t_employee")
public class Emp extends AbstractAuditBase {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer employeeId;
    @Column(nullable = false)
    private String employeeName;
    @Column(nullable = false,unique = true)
    private String employeeAccount;
    @Column(nullable = false)
    private String employeePwd;
    @Column(nullable = false)
    private String employeeTel;
    @Column(columnDefinition = "tinyint(1) default 1")
    private Boolean empStatus;

    @OneToMany(mappedBy = "emp", cascade = CascadeType.ALL) //指定中间表映射变量名
    @JsonIgnore
    private List<EmpRole> empRoles = new ArrayList<>();


    //获取权限列表
    public List<SimpleGrantedAuthority> getAllPermissions(){
        List<SimpleGrantedAuthority> authorities = new ArrayList<>();
        for (EmpRole empRole:empRoles){
            Role role = empRole.getRole();
            if (!Objects.isNull(role)){
                role.getPermissions().forEach(permission -> {
                    //String roleName = "Role_"+role.getRoleName(); //角色名
                    String permissionName = permission.getPermitName();
                    log.info(permissionName);
                    authorities.add(new SimpleGrantedAuthority(permissionName));
                });
            }
        }
        return authorities;
    }

}
